const { Sequelize } = require('sequelize');
const UserModel = require('./models/users');

// Option 1: Passing parameters separately
const sequelize = new Sequelize('nyjm', 'root', 'root', {
  host: 'localhost',
  dialect: 'mysql', /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' | 'sqlite' */
  timezone: '+08:00',     // 这里是东八区，默认为0时区
  pool: {                 // 使用连接池
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000,
  },
  define: {
    timestamps: false
  }
});

// 测试连接是否成功
// (async () => {
//   try {
//     await sequelize.authenticate();
//     console.log('Connection has been established successfully.');
//   } catch (error) {
//     console.error('Unable to connect to the database:', error);
//   }
// })();   // 多一个括号表示调用方法


const userModel = UserModel(sequelize);
// // const users = /*await*/ userModel.get('username');
// // console.log(users.dataValues);
// sequelize.sync({
//   force: true
// }).then(() => userModel.find({
//   where: {
//     username: 'admin'
//   }
// }).then(console.log))

async function findAllUsers() {
  try {
    const users = await userModel.findAll({
      attributes: ['username', 'password', 'nickname'],
      where: {
        username: 'admin'
      }
    });
    // console.log("All Users:", JSON.stringify(users, null, 2));
    console.log(users[0].dataValues)
  } catch(error) {
    console.log("操作失败！\n" + error);
  }
}

findAllUsers();

